package fr.cephb.joperon.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JMenu;

import javax.swing.AbstractAction;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import fr.cephb.joperon.core.bio.Assembly;
import fr.cephb.joperon.core.bio.Chromosome;
import fr.cephb.joperon.core.entities.SeqMap;
import fr.cephb.swing.GenericTableModel;
import fr.cephb.swing.ObjectAction;


public class SeqMapFrame extends  OperonFrame
	{
	private static final long serialVersionUID = 1L;

	private static class Model
		extends GenericTableModel<SeqMap>
		{
		private static final long serialVersionUID = 1L;
		@Override
		public int getColumnCount() {
			return 4;
			}
		@Override
		public Object getValueOf(SeqMap item, int columnIndex) {
			switch(columnIndex)
				{
				case 0: return item.getAcn();
				case 1: return item.getP();
				case 2: return item.getQ();
				case 4: return null;
				}
			return null;
			}
		}
	private Assembly assembly;
	private Model tableModel;
	private JTable table;
	private SimpleRegionRenderer render;
	public SeqMapFrame(OperonGUI operon,Assembly assembly)
		{
		super(operon,"SeqMap : "+assembly.getName());
		this.assembly=assembly;
		JMenu menu= new JMenu("Chromosomes");
		getJMenuBar().add(menu);
		for(Chromosome chr: Chromosome.values())
			{
			menu.add(new ObjectAction<Chromosome>(chr)
				{
				private static final long serialVersionUID = 1L;
				@Override
				public void actionPerformed(ActionEvent e) {
					setChromosome(getObject());
					}
				});
			}
		
		menu= new JMenu("Action");
		getJMenuBar().add(menu);
		AbstractAction snpMap= new AbstractAction("SNP")
			{
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e)
				{
				int i=table.getSelectedRow();
				i= table.convertRowIndexToModel(i);
				if(i==-1) return;
				SeqMap sm=tableModel.elementAt(i);
				
				SnpMapFrame f= new SnpMapFrame(getOperonGUI(),getAssembly());
				SeqMapFrame.this.getDesktopPane().add(f);
				f.setPosition(Chromosome.getByName(sm.getChr()),
						(int)sm.getP(),
						(int)sm.getQ());
				f.setVisible(true);
				}
			};
		menu.add(snpMap);
		
		this.tableModel= new Model();
		this.table= new JTable(this.tableModel);
		this.render= new SimpleRegionRenderer(1,2);
		this.table.getColumnModel().getColumn(3).setCellRenderer(this.render);
		super.content.add(new JScrollPane(this.table),BorderLayout.CENTER);
		
		this.addInternalFrameListener(new InternalFrameAdapter()
			{
			@Override
			public void internalFrameOpened(InternalFrameEvent e) {
				setChromosome(Chromosome.Chr1);
				}
			});
		selectionListen(snpMap,this.table);
		}
	
	public Assembly getAssembly() {
		return assembly;
		}
	
	public void setChromosome(Chromosome chr)
		{/*
		SeqMabDB db=null;
		try {
			db = new SeqMabDB(getOperon(),getAssembly());
			db.open();
			this.tableModel.removeAll();
			this.tableModel.addAll(db.list(chr).asList());
			int min=Integer.MAX_VALUE;
			int max=0;
			for(int i=0;i< this.tableModel.getRowCount();++i)
				{
				min=Math.min( min,this.tableModel.elementAt(i).getP());
				max=Math.max( max,this.tableModel.elementAt(i).getQ());
				}
			this.render.setChromMin(min);
			this.render.setChromMax(max);
			} 
		catch (Exception e)
			{
			e.printStackTrace();
			}
		finally
			{
			safeClose(db);
			}
		*/
		}
	
	}
